//
//  KZMineUserInfoHeader.swift
//  BoyeStore
//
//  Created by 朱凯 on 2018/6/25.
//  Copyright © 2018年 itboye. All rights reserved.
//

import UIKit

import RxCocoa
import RxSwift



class KZMineUserInfoHeader: UIView {
    
    let bgView = UIView()
    let iconView = UIImageView()        // 头像
    let nickLabel = UILabel()           // 昵称
    let withdrawLabel = UILabel()       // 可提现金额
    var bottomLabel1 = UILabel()        // 累计返利
    var bottomLabel2 = UILabel()        // 来自邀请分润金额
    var bottomLabel3 = UILabel()        // 邀请用户
    
    var offset: CGFloat = 0 {
        didSet {
            guard offset < 0 else { return }
            setNeedsLayout()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: ------ 拓展 rx
extension Reactive where Base: KZMineUserInfoHeader {
    internal var offset: Binder<CGFloat> {
        return Binder<CGFloat>.init(self.base, binding: { (header, offset) in
            header.offset = offset
        })
    }
}

// MARK: ------ 创建视图
extension KZMineUserInfoHeader {
    
    override func layoutSubviews() {
        bgView.snp.updateConstraints { (m) in
            m.height.equalTo(self).offset(-offset)
            m.top.equalTo(offset)
        }
    }
    
    private func setupUI() {
        
        self.addSubview(bgView)
        bgView.backgroundColor = kThemeColor
        bgView.snp.makeConstraints { (m) in
            m.left.right.top.height.equalTo(self)
        }
        
        self.addSubview(iconView)
        iconView.image = kDefaultImage
        iconView.snp.makeConstraints { (m) in
            m.top.equalTo(60)
            m.left.equalTo(40)
            m.width.height.equalTo(60)
        }
    
        nickLabel.textColor = UIColor.white
        nickLabel.numberOfLines = 2
        nickLabel.textAlignment = .left
        nickLabel.text = "Nicksdfslfjsldfjslkdfjslfjslkfjlk"
        nickLabel.font = UIFont.systemFont(ofSize: 14)
        self.addSubview(nickLabel)
        nickLabel.snp.makeConstraints { (m) in
            m.centerY.equalTo(iconView)
            m.left.equalTo(iconView.snp.right).offset(10)
        }
        
        withdrawLabel.textColor = kThemeColor
        withdrawLabel.adjustsFontSizeToFitWidth = true
        withdrawLabel.backgroundColor = UIColor.white
        withdrawLabel.textAlignment = .center
        withdrawLabel.text = "可提现金额 666"
        withdrawLabel.font = UIFont.systemFont(ofSize: 11)
        self.addSubview(withdrawLabel)
        withdrawLabel.snp.makeConstraints { (m) in
            m.right.equalTo(0)
            m.height.equalTo(30)
            m.width.equalTo(100)
            m.centerY.equalTo(iconView)
            m.left.greaterThanOrEqualTo(nickLabel.snp.right).offset(10)
        }
        
        let titles = ["累计返利", "来自邀请分润金额", "邀请用户"]
        var last: UILabel? = nil
        for (i, title) in titles.enumerated() {
            
            let valueLabel = UILabel()
            valueLabel.textColor = UIColor.white
            valueLabel.adjustsFontSizeToFitWidth = true
            valueLabel.textAlignment = .center
            valueLabel.text = "111"
            valueLabel.font = UIFont.systemFont(ofSize: 13)
            self.addSubview(valueLabel)
            valueLabel.snp.makeConstraints { (m) in
                
                m.width.equalTo(self).multipliedBy(1.0 / 3)
                m.top.equalTo(iconView.snp.bottom).offset(36)
                m.left.equalTo(last?.snp.right ?? self.snp.left)
            }
            
            switch i {
            case 0:
                bottomLabel1 = valueLabel
            case 1:
                bottomLabel2 = valueLabel
            case 2:
                bottomLabel3 = valueLabel
            default:
                print(1)
            }
            
            last = valueLabel
            
            let titleLabel = UILabel()
            titleLabel.textColor = UIColor.white
            titleLabel.adjustsFontSizeToFitWidth = true
            titleLabel.textAlignment = .center
            titleLabel.text = title
            titleLabel.font = UIFont.systemFont(ofSize: 13)
            self.addSubview(titleLabel)
            titleLabel.snp.makeConstraints { (m) in
                
                m.width.height.left.equalTo(valueLabel)
                m.top.equalTo(valueLabel.snp.bottom).offset(10)
            }
        }
        
        
        self.layoutIfNeeded()
        let withdrawMask = CAShapeLayer()
        withdrawMask.backgroundColor = UIColor.white.cgColor
        let path = UIBezierPath(roundedRect: withdrawLabel.bounds, byRoundingCorners: [.topLeft, .bottomLeft], cornerRadii: CGSize(width: 20, height: 20))
        withdrawMask.path = path.cgPath
        withdrawLabel.layer.mask = withdrawMask
        
        
        // 虚线
        let dottedLayer = CAShapeLayer()
        dottedLayer.lineWidth = 1
        dottedLayer.strokeColor = UIColor.white.cgColor
        dottedLayer.lineDashPattern = [1, 1]
        let dotPath = UIBezierPath()
        dotPath.move(to: CGPoint(x: 20, y: iconView.bottom + 16))
        dotPath.addLine(to: CGPoint(x: self.width - 20, y: iconView.bottom + 16))
        dottedLayer.path = dotPath.cgPath
        self.layer.addSublayer(dottedLayer)
        
        
    }
    
    
}

// 事件
extension KZMineUserInfoHeader {
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let vc = KZMineInformationVC()
        self.viewController?.navigationController?.pushViewController(vc, animated: true)
    }
}
